package com.roads.screen.controller;

import com.roads.common.core.domain.AjaxResult;
import com.roads.common.core.domain.chart.BaseChartModel;
import com.roads.common.utils.StringUtils;
import com.roads.project.domain.ProBoq;
import com.roads.project.domain.ProInstitution;
import com.roads.project.domain.ProPaymentItems;
import com.roads.project.service.IProBoqService;
import com.roads.project.service.IProInstitutionService;
import com.roads.project.service.IProPaymentItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 可视化大屏1数据Controller
 *
 * @author nhp
 * @date 2023-10-10
 */
@RestController
@RequestMapping("/screen/screen1/data")
public class Screen1DataController {

    @Autowired
    private IProPaymentItemsService paymentItemsService;

    @Autowired
    private IProInstitutionService institutionService;

    @Autowired
    private IProBoqService boqService;

    /**
     * 获取项目百章金额饼状图数据
     *
     * @param projectId 项目ID
     * @return 结果
     */
    @RequestMapping("/getProjectHundredChaptersTotalChartPie/{projectId}")
    public AjaxResult getProjectHundredChaptersTotalChartPie(@PathVariable("projectId") Integer projectId) {
        AjaxResult ajaxResult = new AjaxResult();
        ProPaymentItems search = new ProPaymentItems();
        search.setProjectId(projectId);
        search.setComputeMode(1);
        search.setItemStatus("0");
        List<ProPaymentItems> items = paymentItemsService.selectModelList(search);
        List<ProInstitution> bids = institutionService.selectBidListByProjectId(projectId);
        List<BaseChartModel> data = new ArrayList<>();
        List<String> legend = new ArrayList<>();
        for (ProPaymentItems item : items) {
            BaseChartModel chart = new BaseChartModel();
            chart.setName(item.getItemCode() + "章");
            legend.add(item.getItemCode() + "章");
            BigDecimal total = BigDecimal.valueOf(0);
            for (ProInstitution bid : bids) {
                String institutionCode = bid.getInnerCode();
                ProBoq boq = boqService.selectProBoqByCode(institutionCode, item.getItemCode());
                if (StringUtils.isNotNull(boq)) {
                    BigDecimal boqTotal = boq.getTotal() == null ? BigDecimal.valueOf(0) : boq.getTotal();
                    total = total.add(boqTotal);
                }
            }
            total = total.divide(BigDecimal.valueOf(10000),0, BigDecimal.ROUND_HALF_UP);
            chart.setValue(total.intValue());
            data.add(chart);
        }
        ajaxResult.put("legend", legend);
        ajaxResult.put("data", data);
        return ajaxResult;
    }

}
